//
//  HLCircleProgressView.m
//  MOSOBOStudent
//
//  Created by huangqinghua on 2017/10/23.
//  Copyright © 2017年 huangqinghua. All rights reserved.
//

#import "HLCircleProgressView.h"
#import "UIView+EXTENSION.h"
#import "HLCountingLabel.h"

@interface HLCircleProgressView()
///进度提示lab
@property (nonatomic, strong)HLCountingLabel *progressTipLab;
@property (nonatomic, strong) UILabel *tipLab;
///当前原点
@property (nonatomic, strong) UIView *pointView;
//进度圆环
@property (nonatomic, strong) CAShapeLayer *shapeLayer;
///当前进度
@property (nonatomic, assign) CGFloat progress;
///原点当前位置
@property (nonatomic, assign) CGFloat currentPointAngle;

@end

@implementation HLCircleProgressView

- (instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        self.backgroundColor = [UIColor whiteColor];
        [self setUpUI];
    }
    return self;
}


- (void)setUpUI{
    [self addSubview:self.progressTipLab];
    [self addSubview:self.tipLab];
    //圆路径
    UIBezierPath *circlePath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.width / 2, self.height / 2) radius:(self.width - 10)/2 startAngle:M_PI / 4 + M_PI / 2 endAngle:M_PI / 4 clockwise:YES];
    CAShapeLayer *bgLayer = [CAShapeLayer layer];
    bgLayer.frame = self.bounds;
    bgLayer.fillColor = [UIColor clearColor].CGColor;//填充色 -  透明色
    bgLayer.lineWidth = 10.f;
    bgLayer.strokeColor = ZCCRGBColor(212, 212, 212, 1.0).CGColor;//线条颜色
    bgLayer.strokeStart = 0;
    bgLayer.strokeEnd = 1;
    bgLayer.lineCap = kCALineCapRound;
    bgLayer.path = circlePath.CGPath;
    [self.layer addSublayer:bgLayer];
    
    _shapeLayer = [CAShapeLayer layer];
    _shapeLayer.frame = self.bounds;
    _shapeLayer.fillColor = [UIColor clearColor].CGColor;
    _shapeLayer.lineWidth = 10.f;
    _shapeLayer.lineCap = kCALineCapRound;
//    _shapeLayer.strokeColor = color.CGColor;
    _shapeLayer.strokeColor = [UIColor blueColor].CGColor;
    _shapeLayer.strokeStart = 0;
    _shapeLayer.strokeEnd = 0;
    _shapeLayer.path = circlePath.CGPath;
    [self.layer addSublayer:_shapeLayer];
    
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.backgroundColor = [UIColor blueColor].CGColor;
    [gradientLayer setMask:_shapeLayer];
    gradientLayer.frame = self.bounds;
    [self.layer addSublayer:gradientLayer];
    [self addSubview:self.pointView];
}

- (void)animateToProgress:(CGFloat)progress{
    if (progress > self.progress && progress > 1) return;
    [self.pointView.layer removeAllAnimations];
    [self.shapeLayer removeAllAnimations];
    CGFloat duration = 2*(progress - self.progress);
    //数字动画
    [self.progressTipLab countingFrom:self.progress*100 to:progress*100 duration:duration];
    //线条动画
    CABasicAnimation *pathAnima = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    pathAnima.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    pathAnima.duration = duration;
    pathAnima.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    pathAnima.fromValue = @(self.progress);
    pathAnima.toValue = [NSNumber numberWithFloat:progress];
    pathAnima.fillMode = kCAFillModeForwards;
    pathAnima.removedOnCompletion = NO;
    [self.shapeLayer addAnimation:pathAnima forKey:@"strokeEndAnimation"];
    ///原点动画
    CAKeyframeAnimation *pathAnimation_1 = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    pathAnimation_1.calculationMode = kCAAnimationPaced;
    pathAnimation_1.fillMode = kCAFillModeForwards;
    pathAnimation_1.removedOnCompletion = NO;
    pathAnimation_1.duration = duration;
    pathAnimation_1.repeatCount = 0;
    pathAnimation_1.values = @[@(progress)];
    CGFloat total = (M_PI + 1/2.f *M_PI);
    CGFloat start = M_PI / 4 + M_PI / 2;
    CGFloat end = (start + (total *progress)) - 2*M_PI > 0 ? (start + (total *progress)) - 2*M_PI : (start + (total *progress));
    UIBezierPath *circlePath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.width / 2, self.height / 2) radius:(self.width - 10)/2 startAngle:self.currentPointAngle endAngle:end clockwise:YES];
    pathAnimation_1.path = circlePath.CGPath;
    [self.pointView.layer addAnimation:pathAnimation_1 forKey:@"movePoint"];
    self.progress = progress;
    self.currentPointAngle = end;
    
}

#pragma mark - set/get

- (CGFloat)currentPointAngle {
    if (_currentPointAngle == 0) {
        return M_PI / 4 + M_PI / 2;
    }
    return _currentPointAngle;
}

- (HLCountingLabel *)progressTipLab{
    if(_progressTipLab == nil){
        //半径
        _progressTipLab = [[HLCountingLabel alloc] initWithFrame:CGRectMake(0, 0, self.width, 20)];
        _progressTipLab.textAlignment = NSTextAlignmentCenter;
        _progressTipLab.font = [UIFont boldSystemFontOfSize:18];
//        #FFA556
        _progressTipLab.textColor = [UIColor orangeColor];
        _progressTipLab.text = @"0%";
        _progressTipLab.centerY = self.height/2.f - 10;
    }
    return _progressTipLab;
}

- (UILabel *)tipLab {
    if(_tipLab == nil){
        //半径
        _tipLab = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.progressTipLab.frame) + 10, self.width, 20)];
        _tipLab.textAlignment = NSTextAlignmentCenter;
        _tipLab.font = ZCCFontPingFangSCLight(14);
//        #A5A5A5
        _tipLab.textColor = [UIColor lightGrayColor];
        _tipLab.text = @"学习进度";
    }
    return _tipLab;
}

- (UIView *)pointView{
    if (_pointView == nil) {
        CGFloat radius = self.width / 2;
        CGFloat offsetWH = 10/1.414/2;
        _pointView = [[UIView alloc]init];
        _pointView.frame = CGRectMake(0, 0, 10, 10);
        CGPoint center = CGPointMake((self.width - radius * 1.414) / 2 + offsetWH , radius / 1.414 + radius - offsetWH);
        _pointView.center = center;
        _pointView.backgroundColor = [UIColor redColor];
        _pointView.layer.cornerRadius = 5;
        _pointView.layer.masksToBounds = YES;
    }
    return _pointView;
}


@end
